package logic

import (
	"2210a/user-profile/models/mysql"
	"context"
	"fmt"

	"2210a/user-profile/userprofilerpc/internal/svc"
	"2210a/user-profile/userprofilerpc/userprofilerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *userprofilerpc.LoginRequest) (*userprofilerpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.Db.Debug().Where("username = ?", in.Username).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("用户账号不存在，请重新输入")
	}
	if user.Password != in.Password {
		return nil, fmt.Errorf("密码不一致，请重新输入")
	}

	return &userprofilerpc.LoginResponse{Id: int64(user.ID)}, nil
}
